package com.javacoo.xservice.base.support.handler.internal;

import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.javacoo.xservice.base.support.handler.Handler;
import com.javacoo.xservice.base.support.handler.HandlerStack;

/**
 * 默认处理器链实现
 * <p>说明:</p>
 * <li></li>
 * @author DuanYong
 * @since 2017年7月11日下午1:42:57
 */
@Component
public class DefaultHandlerStack implements HandlerStack {
	/** 处理器列表 */
	@Autowired
	private List<Handler> handlerList;

	@Override
	public void doPreStack(Object context) {
        handlerList.stream().sorted(Comparator.comparing(Handler::index)).forEach(handler -> handler.preHandle(context));
	}


	@Override
	public void doAfterStack(Object context) {
        handlerList.stream().sorted(Comparator.comparing(Handler::index)).forEach(handler -> handler.afterHandle(context));
	}
	
	@Override
	public void addHandler(Handler handler) {
		handlerList.add(handler);
	}

}
